﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class FormPipeSystemTag : System.Windows.Forms.Form
	{
		public FormPipeSystemTag()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{98DDFF9C-CAA7-4F07-8B65-21B620C09458}", null);
			this.userSetting = new UserSetting();
			this.InitData();
		}

		public void SetParaentData(CreatePipeSysText par, Transaction tran)
		{
			this.paraent = par;
			this.trans = tran;
		}

		private void InitData()
		{
			this.radioButton1.Checked = true;
			this.textBox1.Text = "2.5";
			if (!string.IsNullOrEmpty(this.userSetting.GetValue("PipeSystemTag", "Distance")))
			{
				this.textBox1.Text = this.userSetting.GetValue("PipeSystemTag", "Distance");
			}
			if (string.IsNullOrEmpty(this.userSetting.GetValue("PipeSystemTag", "LayType")))
			{
				return;
			}
			int num = int.Parse(this.userSetting.GetValue("PipeSystemTag", "LayType"));
			if (num != 1)
			{
				if (num != 2)
				{
					this.radioButton1.Checked = true;
				}
				else
				{
					this.radioButton2.Checked = true;
				}
			}
			else
			{
				this.radioButton1.Checked = true;
			}
			if (!string.IsNullOrEmpty(this.userSetting.GetValue("PipeSystemTag", "CheckedCustom")) && this.userSetting.GetValue("PipeSystemTag", "CheckedCustom") == "true")
			{
				this.checkBox1.Checked = true;
			}
			else
			{
				this.checkBox1.Checked = false;
			}
			this.textBox2.Text = "J";
			if (!string.IsNullOrEmpty(this.userSetting.GetValue("PipeSystemTag", "SystemName")))
			{
				this.textBox2.Text = this.userSetting.GetValue("PipeSystemTag", "SystemName");
			}
			if (!string.IsNullOrEmpty(this.userSetting.GetValue("PipeSystemTag", "bDelete")) && this.userSetting.GetValue("PipeSystemTag", "bDelete") == "false")
			{
				this.chkDel.Checked = false;
			}
			else
			{
				this.chkDel.Checked = true;
			}
			this.checkBox1_CheckedChanged(null, null);
			this.CheckLayerMode();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (!this.DataCheck())
			{
				return;
			}
			this.paraent.MakeAll(this.trans, this.nDistance);
			this.ChangeOperateState();
			base.Close();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			FormPipeSystemTag.SendMessage(CreatePipeSysText._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FormPipeSystemTag.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormPipeSystemTag.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void radioButton1_CheckedChanged(object sender, EventArgs e)
		{
			this.nLayType = 1;
			this.CheckLayerMode();
		}

		private void radioButton2_CheckedChanged(object sender, EventArgs e)
		{
			this.nLayType = 2;
			this.CheckLayerMode();
		}

		public bool DataCheck()
		{
			this.nDistance = (int)(double.Parse(this.textBox1.Text) * 1000.0);
			if (this.nDistance < 100)
			{
				MessageBox.Show("文字间距太小，请重新设置！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return false;
			}
			if (this.checkBox1.Checked && string.IsNullOrEmpty(this.textBox2.Text))
			{
				return false;
			}
			this.szSysName = this.textBox2.Text;
			this.userSetting.WriteValue("PipeSystemTag", "Distance", this.textBox1.Text);
			this.userSetting.WriteValue("PipeSystemTag", "LayType", this.nLayType.ToString());
			this.userSetting.WriteValue("PipeSystemTag", "CheckedCustom", this.checkBox1.Checked.ToString());
			this.userSetting.WriteValue("PipeSystemTag", "SystemName", this.textBox2.Text);
			this.userSetting.WriteValue("PipeSystemTag", "bDelete", this.chkDel.Checked.ToString());
			if (!this.checkBox1.Checked)
			{
				this.szSysName = null;
			}
			this.CheckLayerMode();
			return true;
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			if (this.checkBox1.Checked)
			{
				this.textBox2.Enabled = true;
				this.label2.Enabled = true;
				return;
			}
			this.textBox2.Enabled = false;
			this.label2.Enabled = false;
		}

		private void FormPipeSystemTag_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.ChangeOperateState();
		}

		private void CheckLayerMode()
		{
			if (!this.radioButton1.Checked)
			{
				this.label1.Enabled = true;
				this.textBox1.Enabled = true;
				this.chkDel.Enabled = false;
				this.bDeletel = true;
				return;
			}
			this.label1.Enabled = false;
			this.textBox1.Enabled = false;
			this.chkDel.Enabled = true;
			if (this.chkDel.Checked)
			{
				this.bDeletel = true;
				return;
			}
			this.bDeletel = false;
		}

		private void FormPipeSystemTag_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
		}

		private void FormPipeSystemTag_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		public int nLayType = 1;

		public int nDistance;

		public string szSysName = "";

		public bool bDeletel = true;

		private UserSetting userSetting;

		private CreatePipeSysText paraent;

		private Transaction trans;

		private ControlInfoManager m_thisManger;
	}
}
